﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AudioCore.Entity;
using AudioCore.DAO;
using System.Data;

namespace AudioCore.Biz
{
    public class ArticlePartBiz : BaseBiz
    {
        public int AddArticlePart(ArticlePartEntity entity)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    return dao.InsertEntity(entity, typeof(ArticlePartEntity));
                }
            }
            catch (Exception e)
            {
                _ts.TraceError(e.ToString());
                //do nothing
            }
            return -1;
        }

        public int UpdateArticlePart(ArticlePartEntity entity)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    return dao.UpdateEntity(entity, typeof(ArticlePartEntity));
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public int DeleteArticlePart(int articlePartID)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    return dao.DeleteEntity(articlePartID);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        #region Method for service
        public ArticlePartEntity GetArticlePartByID(int articlePartID)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    DataTable talbe = dao.SelectEntityByID(articlePartID);
                    if (talbe.Rows.Count > 0)
                    {
                        return (ArticlePartEntity)BaseBiz.MapDataRowToObject(talbe.Rows[0], typeof(ArticlePartEntity));
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        public ArticlePartEntity GetArticlePartByID(int accountID, int articlePartID)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    DataTable table = dao.GetArticlePartByID(accountID, articlePartID);
                    if (table.Rows.Count > 0)
                    {
                        return (ArticlePartEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(ArticlePartEntity));
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return null;
        }

        public ArticlePartEntity GetArticlePartByName(int articleID, string articleName)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    string[] columns = new string[] { "ArticleID", "ArticlePartName" };
                    string[] values = new string[] { "" + articleID, articleName };
                    DataTable table = dao.SelectEntityByColumnNames(columns, values);
                    if (table.Rows.Count > 0)
                    {
                        return (ArticlePartEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(ArticlePartEntity));
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return null;
        }

        public List<ArticlePartEntity> GetArticlePartByArticleID(int articleID)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    DataTable table = dao.SelectEntityByColumnName("ArticleID", articleID);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticlePartEntity)).ConvertAll(item => (ArticlePartEntity) item);
                    }
                }
            }
            catch (Exception e)
            {

            }
            return new List<ArticlePartEntity>();
        }

        public List<ArticlePartEntity> GetArticlePartByPage(int accountID, int articleID, int pageIndex, int pageSize, ref int counter)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    DataTable table = dao.GetArticlePartByPage(accountID, articleID, pageIndex, pageSize, ref counter);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticlePartEntity)).ConvertAll(item => (ArticlePartEntity)item);
                    }
                }
            }
            catch (Exception e)
            {

            }
            return new List<ArticlePartEntity>();
        }

        public int GetLastSortOrder(int articleID)
        {
            try
            {
                using (ArticlePartDao dao = new ArticlePartDao())
                {
                    return dao.GetLastSortOrder(articleID);
                }
            }
            catch (Exception e)
            {

            }
            return 0;
        }
        #endregion
    }
}
